#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include "cgic.h"

int form_get_post();
int fd;

int cgiMain()
{
    char checkValue[100];
    
    fd = open("/etc/my_fifo", O_WRONLY);
    if(fd < 0)
    {
        perror("open");
        return 0;
    }

    cgiHeaderContentType("text/html");
    fprintf(cgiOut, "<HTML><HEAD>\n");
    fprintf(cgiOut, "<TITLE>My CGI</TITLE></HEAD>\n");
    fprintf(cgiOut, "<BODY>");
    cgiFormString("takePhoto", checkValue, 241);
    if(strcmp(checkValue, "1") == 0)
    {
        fprintf(cgiOut, "<H1>TakePhoto Success</H1>\n");
    }
    else
    {
        fprintf(cgiOut, "<H1>TakePhoto Failed</H1>\n");
    }

    fprintf(cgiOut, "<a href=\"scan_dir_cgi.cgi\">Scan_Dir_Web</a>\n");
    fprintf(cgiOut, "<a href=\"/index.html\">Index</a>\n");
    fprintf(cgiOut, "</BODY>\n");
    fprintf(cgiOut, "</HTML>\n");

    write(fd, checkValue, strlen(checkValue));
    
    //form_get_post();
    return 0;
}

#if 0
int main()
{
    char *ret = NULL;
    // http协议
    printf("Content-Type:text/html;charset=utf-8\n\n");
    printf("\n\n");

#if 0
    printf("<head><title>Submit Button</title></head>\n");
    printf("<body>\n");
    //printf("<form action=\"/cgi-bin/mkfifo_cgi.cgi\" method=\"get\">\n");
    printf("<form action=\"#\" method=\"get\">\n");
    printf("<input type=\"submit\" value=\"Click Me!\">\n");
    printf("</form>\n");
    printf("</body>\n</html>");
#endif

    fd = open("/etc/my_fifo", O_WRONLY);
	if(fd < 0)
	{
		perror("open");
		return 0;
	}

    char name[241];
    char checkValue[100];

    fd = open("/etc/my_fifo", O_WRONLY);
    if(fd < 0)
    {
        perror("open");
        return 0;
    }

 
    
    //form_get_post();
    //form_get_post();
    return 0;
}
#endif


#if 1
int form_get_post()
{
    char *ret = NULL;
    char *info = NULL;
    int len, i;

    // 获取请求类型 get或者post
    ret = getenv("REQUEST_METHOD");
    if (ret == NULL)
    {
        printf("获取method失败！");
        return -1;
    }
    printf("<h2>http请求形式：%s</h2>", ret);

    if (strncmp(ret, "GET", 3) == 0)
    {
        // 查看get的请求
        info = getenv("QUERY_STRING");
        if (info == NULL)
        {
            printf("获取query string失败！");
            return -1;
        }
        printf("<h3>%s的请求数据：%s</h3>", ret, info);

        printf("<h3>写端open成功了</h3>\n");
	    write(fd, info, strlen(info));

    }
    else if (strncmp(ret, "POST", 4) == 0)
    {
        // 查看post的请求
        // CONTENT_LENGTH 客户端向标准输入设备发送的数据长度，单位为字节
        len = atoi(getenv("CONTENT_LENGTH"));
        info = (char *)malloc(len + 1);

        for (i = 0; i < len; i++)
        {
            info[i] = (char)fgetc(stdin);
        }
        info[i] = 0;
        printf("<h3>%s的请求数据：%s</h3>", ret, info);
        free(info);
        info = NULL;
    }
    else
    {
        printf("<h3>form请求类型未知</h3>");
        return -1;
    }
    return 0;
}
#endif